package com.woniu.controller;

import com.woniu.pojo.Course;
import com.woniu.pojo.Description;
import com.woniu.pojo.Subject;
import com.woniu.pojo.Video;
import com.woniu.service.ICourseService;
import com.woniu.service.IDescriptionService;
import com.woniu.service.ISubjectService;
import com.woniu.service.IVideoService;
import com.woniu.vo.NewCourseVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/course")
public class CourseController {

    @Autowired
    private ICourseService courseService;
    @Autowired
    private ISubjectService subjectService;

    @Autowired
    private IDescriptionService descriptionService;

    @Autowired
    private IVideoService videoService;

    @Autowired
    private TemplateEngine templateEngine;

    @Value("${params.index.detailCourseNum}")
    private Integer detailCourseNum;


    @GetMapping("/list")
    public String list(String subjectId, Integer currentPage, Model model){

      List<Subject> subjectList = subjectService.listbyParentId();
      List<NewCourseVO> courseList  = courseService.listbySubjectId(currentPage,subjectId);

      model.addAttribute("subjectList",subjectList);
      model.addAttribute("courseList",courseList);
      model.addAttribute("subjectId",subjectId);

      return "course_list";

    }

    @GetMapping("/detail/{courseId}")
    public String todetail(Model model, @PathVariable String courseId){
        //1、查询课程简介数据
        Description courseDescription = descriptionService.getById(courseId);
        //2、查询课程下所有的视频
       List<Video>  videos= videoService.getVideosByCourseId(courseId);
        //3、查询课程数据
        NewCourseVO courseVO = courseService.queryCourseDetilBycourseId(courseId);
        //4、查询精选好课
        List<NewCourseVO>  detailCourseVOList = courseService.queryDetailCourse(detailCourseNum);

        model.addAttribute("courseDescription",courseDescription);
        model.addAttribute("videos",videos);
        model.addAttribute("courseVO",courseVO);
        model.addAttribute("detailCourseVOList",detailCourseVOList);

        return "course_detail";

    }


    @GetMapping("/createhtml/{courseId}")
    @ResponseBody
    public String createhtml(@PathVariable String courseId) throws IOException {

        //1、查询课程简介数据
        Description courseDescription = descriptionService.getById(courseId);
        //2、查询课程下所有的视频
        List<Video>  videos= videoService.getVideosByCourseId(courseId);
        //3、查询课程数据
        NewCourseVO courseVO = courseService.queryCourseDetilBycourseId(courseId);
        //4、查询精选好课
        List<NewCourseVO>  detailCourseVOList = courseService.queryDetailCourse(detailCourseNum);

        //1、创建上下文对象
        Context context =new Context();
        //2、将查询出来的数据设置到该上下文对象中
//        单独一个数据用这个 context.setVariable("courseVO",courseVO);  多个用map
        Map<String, Object> params = new HashMap<>();
        params.put("courseDescription",courseDescription);
        params.put("videos",videos);
        params.put("courseVO",courseVO);
        params.put("detailCourseVOList",detailCourseVOList);
        context.setVariables(params);
        //3、指定静态页面生成的位置
        File file =new File("C:\\Users\\du'du\\Desktop\\one\\3jieduanfengge\\me\\houduan\\wnxt_parent2\\wnxt_user\\src\\main\\resources\\static\\"+
                "course_detail_"+courseId+".html");
        if (!file.exists()){
            file.createNewFile();
        }
        //  4、创建一个输出流对象
        PrintWriter writer = new PrintWriter(file);
        // 5、通过上下文对象，使用输出流生成静态页面
        templateEngine.process("course_detail",context,writer);

        return "生成成功";

    }

    /**
     * 批量生成静态页面
     * */
    @GetMapping("/create")
    @ResponseBody
    public String createHtml() throws IOException {
        courseService.createHtml();
        return "生成静态页面成功";
    }


}
